package com.jz.jingying.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateTools {

	private static class SingletonHolder {
		private static DateTools INSTANCE = new DateTools();
	}

	public static DateTools getInstance() {
		return SingletonHolder.INSTANCE;
	}
	
	public static final String DAY = "day";
	
	public static final String HOUR = "hour";
	
	public static final String MINUTE = "minute";
	
	public static final String SECOND = "second";
	
	public static final String MILLISECOND = "milliSecond";
	
	/**
	 * 格式化日期
	 * @param date
	 * @param formatString "yyyy-MM-dd HH:mm:ss"
	 * @return
	 */
	public String dateFormat(Date date,String formatString){
		DateFormat format = new SimpleDateFormat(formatString);
		return format.format(date);
	}
	/**
	 * 格式化日期
	 * @param date
	 * @param formatString "yyyy-MM-dd HH:mm:ss"
	 * @return
	 */
	public String dateFormatZero(String date){
		
		Date d = null;
		if(date != null && !"".equals(date) && date.length() > 8){
			d = this.stringFormat(date, "yyyy-MM-dd");
		}else{
			d = this.stringFormat(date, "yyyyMMdd");
		}
		String format2 = this.dateFormat(d, "yyyy-MM-dd HH:mm:ss");
		return format2;
	}
	
	/**
	 * 格式化日期字符串
	 * @param str
	 * @param formatString "yyyy-MM-dd HH:mm:ss"
	 * @return
	 */
	public Date stringFormat(String str,String formatString){
		DateFormat format = new SimpleDateFormat(formatString);
		Date date = new Date();
		try {
			date = format.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date; 
	}
	
	/**
	 * 获取当前零点日期
	 * @param now
	 * @return
	 */
	public Date setZeroTime(Date now){
		Date date = null;
		if(null != now){
			String str = dateFormat(now, "yyyy-MM-dd");
			date = stringFormat(str, "yyyy-MM-dd");
		}
		if(null == date){
			date = new Date(0L);
		}
		return date;
	}
	
	/**
	 * 修改日期时间
	 * @param date
	 * @param ChangeType Calendar
	 * @param ChangeNO
	 * @return
	 */
	public Date changeDateTime(Date date,int ChangeType,int ChangeNO){
		Calendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(ChangeType, ChangeNO);
		return cal.getTime();
	}
	
	/**
	 * 
	 * @param date
	 * @param type Calendar.
	 * @return
	 */
	public int getDateNum(Date date,int type){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(type);
	}
	
	/**
	 * 格式化时间
	 * @param ms
	 * @param type
	 * @return
	 */
	public String formatTime(Long ms,String type,boolean sfms){
		
		if(MILLISECOND.equals(type)){
			sfms = true;
		}
		
		Integer ss = 1000;  
	    Integer mi = ss * 60;  
	    Integer hh = mi * 60;  
	    Integer dd = hh * 24;  
	  
	    Long day = 0l;  
	    Long hour = 0l;  
	    Long minute = 0l;  
	    Long second = 0l;
	    Long milliSecond = 0l;
	    
	    if(DAY.equals(type)){
	    	day = ms / dd;
	    	hour = (ms - day * dd) / hh;
	    	minute = (ms - day * dd - hour * hh) / mi;
	    	second = (ms - day * dd - hour * hh - minute * mi) / ss;
	    }
	    if(HOUR.equals(type)){
	    	hour = (ms - day * dd) / hh;
	    	minute = (ms - day * dd - hour * hh) / mi;
	    	second = (ms - day * dd - hour * hh - minute * mi) / ss;
	    }
	    if(MINUTE.equals(type)){
	    	minute = (ms - day * dd - hour * hh) / mi;
	    	second = (ms - day * dd - hour * hh - minute * mi) / ss;
	    }
	    if(SECOND.equals(type)){
	    	second = (ms - day * dd - hour * hh - minute * mi) / ss;
	    }
	    milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
	    
	    StringBuffer sb = new StringBuffer();  
	    if(day > 0) {  
	        sb.append(day+"天");  
	    }  
	    if(hour > 0) {  
	        sb.append(hour+"小时");  
	    }  
	    if(minute > 0) {  
	        sb.append(minute+"分");  
	    }  
	    if(second > 0) {  
	        sb.append(second+"秒");  
	    }  
	    if(sfms){
	    	if(milliSecond > 0) {  
		        sb.append(milliSecond+"毫秒");  
		    } 
	    }
	    return sb.toString();
	}
	
	/**
     * 得到date2比date1多的天数
     * @param date1    
     * @param date2
     * @return    
     */
	public int betweenDays(Date beginDate ,Date endDate){

		Calendar beginCalendar = Calendar.getInstance();
		beginCalendar.setTime(beginDate);
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(endDate);
		long beginTime = beginCalendar.getTime().getTime();
		long endTime = endCalendar.getTime().getTime();
		int betweenDays = (int) ((endTime - beginTime) / (1000 * 60 * 60 * 24));//先算出两时间的毫秒数之差大于一天的天数  

		endCalendar.add(Calendar.DAY_OF_MONTH, -betweenDays);//使endCalendar减去这些天数，将问题转换为两时间的毫秒数之差不足一天的情况  
		endCalendar.add(Calendar.DAY_OF_MONTH, -1);//再使endCalendar减去1天  
		if (beginCalendar.get(Calendar.DAY_OF_MONTH) == endCalendar.get(Calendar.DAY_OF_MONTH))//比较两日期的DAY_OF_MONTH是否相等  
			return betweenDays + 1; //相等说明确实跨天了  
		else
			return betweenDays + 0; //不相等说明确实未跨天  
	}
	
	public String[] getTodayAndTomorrow(Date now){
		String[] dateArray = new String[2];
		Date todayZero = setZeroTime(now);
		Date tomorrowZero = changeDateTime(todayZero, Calendar.DATE, 1);
		String today = dateFormat(todayZero, "yyyy-MM-dd HH:mm:ss");
		String tomorrow = dateFormat(tomorrowZero, "yyyy-MM-dd HH:mm:ss");
		dateArray[0] = today;
		dateArray[1] = tomorrow;
		return dateArray;
	}
	
	
	public static void main(String[] args) throws Exception {
		Date yxrq = new SimpleDateFormat("yyyy-MM-dd").parse("2018-5-03 00:00:00.000");
		
		System.out.println(DateTools.getInstance().betweenDays(new Date(),yxrq));
	}
}